package org.apache.jsp.procesa;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.io.*;
import java.util.*;
import beans.BeanEnlaces;
import mapping.Enlace;
import mapping.UrlEnlace;

public final class procesabusquedaenlace_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=utf-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			"../error/errorpage.jsp", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write('\n');
      out.write('\n');
      out.write('\n');
      beans.BeanEnlaces mybean = null;
      synchronized (_jspx_page_context) {
        mybean = (beans.BeanEnlaces) _jspx_page_context.getAttribute("mybean", PageContext.PAGE_SCOPE);
        if (mybean == null){
          mybean = new beans.BeanEnlaces();
          _jspx_page_context.setAttribute("mybean", mybean, PageContext.PAGE_SCOPE);
        }
      }
      out.write("\n");
      out.write("<br/><br/>\n");
      out.write("<div id = \"resultBusqEnlaces\">\n");
		
		mybean.conectar();
		int resultadosporpagina = 10;
		final int MAX_LENGTH = 60;
		int inicTupla = Integer.parseInt(request.getParameter("iniciotupla"));
		System.out.println("!!!resultados :  " + resultadosporpagina + " <-> " + inicTupla + "!!!");
		String [] tipoEnlacesElegidos = request.getParameterValues("tipo_enlace");
		/*for(int i = 0 ; tipoEnlacesElegidos!=null && i<tipoEnlacesElegidos.length; i++){
			System.out.println("##########" + tipoEnlacesElegidos[i]);
		}*/
		String nombreEnlace = request.getParameter("nom_enlace");
		String urlenlace = request.getParameter("url_enlace");
		List listEnlaces = mybean.getEnlaces(nombreEnlace, tipoEnlacesElegidos, urlenlace, resultadosporpagina, inicTupla);
		if(listEnlaces == null) {
	       		out.println("<p>ERROR</p>");
		} else if(!listEnlaces.isEmpty()) {
      out.write("\n");
      out.write("\t\t\t<table border=\"1\" width=\"91%\" cellpadding=\"0\" cellspacing=\"0\">\n");
      out.write("\t\t\t<tbody id=\"cuerpoTablaEnlaces\">\n");
      out.write("\t        <tr class=\"trcabeclista\">\n");
      out.write("\t            <td id=\"tdnomenlace\"><b>NOMBRE</b></td>\n");
      out.write("\t            <td id=\"tdurlenlace\"><b>URL</b></td>\n");
      out.write("\t            <td id=\"tdtipoenlace\"><b>TIPO</b></td>\n");
      out.write("\t        </tr>\n");
      out.write("\t\t\t");

			Iterator it2 = listEnlaces.iterator();
			while(it2.hasNext()) {
				Enlace enl = (Enlace) it2.next();
			
      out.write("\n");
      out.write("\t\t\t<tr class=\"listado\" id = \"fila");
      out.print(enl.getId_enlace());
      out.write("\" valign=\"top\" \n");
      out.write("\t\t\t\tonmouseover=\"iluminaFila(this)\" onmouseout=\"oscureceFila(this)\">\n");
      out.write("\t\t\t\t<td>");
      out.print(enl.getEnlace());
      out.write("</td>\n");
      out.write("\t\t\t\t<td>\n");
      out.write("\t\t       \t\t");
List listaUrls = enl.getListaUrls();
		              	if(listaUrls != null){
		                	Iterator it = listaUrls.iterator();
		                    while(it.hasNext()) {
		                    	UrlEnlace url = (UrlEnlace) it.next();
		                        String str_url = url.getUrl_enlace();
		                        if(str_url.trim().length() >= MAX_LENGTH) {
		                        	str_url = str_url.substring(0, MAX_LENGTH + 1) + "...";
		                        }	
		                        String desc_url = url.getDescripcion_url();
		                        if(desc_url == null) 
		                        	desc_url = "&nbsp;";
		            
      out.write("\n");
      out.write("\t\t            <div class=\"tdurl\"><a href=\"");
      out.print(str_url);
      out.write("\" target=\"_blank\" title=\"");
      out.print(desc_url);
      out.write('"');
      out.write('>');
      out.print(str_url);
      out.write("</a></div>\n");
      out.write("\t                         ");
} // fin while
	                    } // fin if
	                
      out.write("   \n");
      out.write("\t       \t\t</td>\n");
      out.write("\t       \t\t<td>");
      out.print( enl.getTipo_enlace());
      out.write("</td>\n");
      out.write("\t       \t</tr>\n");
      out.write("\t       \t");
}//fin while
	       	
      out.write("\n");
      out.write("\t       \t</tbody>\n");
      out.write("\t\t</table>\n");
      out.write("\t\n");
      out.write("\t    ");
}//fin else if
	    else {
      out.write("\n");
      out.write("\t    <center><p>No se han encontrado resultados para su b&uacute;squeda</p></center>\n");
      out.write("\t\t");
}  
   		mybean.desconectar();
   		
      out.write(" \n");
      out.write("</div>\n");
      out.write(" <div class=\"centrado\">\n");
      out.write("   \t");

	if(inicTupla > 1){
	
      out.write("\n");
      out.write("    \t<button class=\"mybtn\" id=\"ant\" onclick=\"retrocedeCursorResultSet('lista_enlaces','form_enlaces','procesabusquedaenlace.jsp','get',");
      out.print(resultadosporpagina);
      out.write(")\">Anterior</button>\n");
      out.write("    ");

	}
	if(inicTupla+resultadosporpagina < mybean.contadorTuplas){
	
      out.write("\n");
      out.write("    \t<button class=\"mybtn\" id=\"sig\" onclick=\"adelantaCursorResultSet('lista_enlaces','form_enlaces','procesabusquedaenlace.jsp','get',");
      out.print(resultadosporpagina);
      out.write(")\">Siguiente</button>\n");
      out.write("    ");

	}
	mybean.desconectar();
 
      out.write("\n");
      out.write("</div>\t\n");
      out.write("<div id=\"res_borrado\"></div>\n");
      out.write("<div id=\"modenlace\"></div>\n");
      out.write("\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
